﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
namespace logicunivesityinventorystore.Store
{
    public partial class ViewDisbursementListDET : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
      
            InitializeComponet();
            
        }

        private void InitializeComponet()
        {


            if (gdDisbursementList.Rows.Count == 0)
            {
                lbDisbursement.Visible = false;
                btnCollected.Visible = false;
                ScriptManager.RegisterStartupScript(this, typeof(string), "Error", "alert('There is no disbursement list now!');window.location.href = 'HomePage_Store.aspx'", true);
            }

        }

        protected void gdDisbursementList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Selected")
            {
                RetrivalListFacade retrival = new RetrivalListFacade();
                ItemFacade item = new ItemFacade();
                //LinkButton retrievalID =(LinkButton) gdDisbursementList.FindControl("lbtnDisNo");
                string rID = e.CommandArgument.ToString();
                //foreach (RetrivalListDetail rDetail in retrival.FindRetrievalDetails(rID))
                //{
                //    Item i=item.FindItemByID(rDetail.ItemCode);
                //    int stock=i.Stock+Convert.ToInt32(rDetail.ActualQty);
                //    int logicStock = i.LogicStock + Convert.ToInt32(rDetail.ActualQty);
                //    item.UpdateStock(rDetail.ItemCode, stock);
                //    item.UpdateLogicStock(rDetail.ItemCode,logicStock);
                    
 
                //}

                retrival.UpdateStatus(e.CommandArgument.ToString(), "Uncollected");
               
                retrival.UpdateBlackList(e.CommandArgument.ToString());
                gdDisbursementList.DataBind();
                InitializeComponet();

            
            }
           
        }

        protected void btnCollected_Click(object sender, EventArgs e)
        {
            bool checkedStatus = false;
            foreach (GridViewRow row in gdDisbursementList.Rows)
            {

                CheckBox cbCollect = (CheckBox)row.FindControl("cbCollected");
                if (cbCollect.Checked)
                {
                    checkedStatus = true;
                    LinkButton lbtnNo = (LinkButton)row.FindControl("lbtnDisNo");
                    string disNo = lbtnNo.Text;
                    new RetrivalListFacade().UpdateStatus(disNo, "Collected");
                    gdDisbursementList.DataBind();
                    InitializeComponet();
 
                }

            }
            if (!checkedStatus)
            {
                ScriptManager.RegisterStartupScript(this, typeof(string), "Error", "alert('No item is selected!');window.location.href = 'ViewDisbursementListDET.aspx'", true);
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }

        protected void gdDisbursementList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string retrievalListID = (e.Row.FindControl("lbtnDisNo") as LinkButton).Text;
                (e.Row.FindControl("lbtnDisNo") as LinkButton).Attributes.Add("onClick", "ShowEditModal('DisbursementDetails.aspx?RetrievalListID=" + retrievalListID + "');");
               
            }
        }

  

   
    }
}